package com.dqx.haoqiu.mvp.module.user;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BaseActivity;
import com.dqx.haoqiu.repository.entity.NewsEntity;
import com.dqx.haoqiu.repository.entity.UserEntity;
import com.dqx.haoqiu.mvp.module.user.dagger.DaggerUserComponent;
import com.dqx.haoqiu.mvp.module.user.dagger.UserModule;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dqx.haoqiu.mvp.common.base.BaseApplication.getAppComponent;

/**
 * 用户页面
 */
public class UserActivity extends BaseActivity<UserPresenter> implements UserContract.View {
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.rv_news)
    RecyclerView rvNews;

    NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);

        adapter = new NewsAdapter();
        rvNews.setLayoutManager(new LinearLayoutManager(this));
        rvNews.setAdapter(adapter);

        //获取用户数据
        presenter.getUserInfo();
        //获取新闻数据
        presenter.getNews(1, 1, 20, System.currentTimeMillis(), 0);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .userModule(new UserModule(this))
                .build().inject(this);
    }

    /**
     * 显示用户数据
     *
     * @param entity
     */
    @Override
    public void showUserInfo(UserEntity entity) {
        tvName.setText("用户名：" + entity.getNickname());
        tvMobile.setText("手机号：" + entity.getMobile());
        Glide.with(this)
                .load(entity.getAvatar())
                .error(R.mipmap.ic_launcher)
                .into(iv);
    }

    /**
     * 显示新闻数据
     *
     * @param entities
     */
    @Override
    public void showNews(List<NewsEntity> entities) {
        adapter.setNewData(entities);
    }

    /**
     * 设置标题
     *
     * @return
     */
    @Override
    public String setTitle() {
        return getString(R.string.user);
    }
}
